<?php

declare(strict_types=1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\TrandingbankListsr as M;
use app\common\validate\TrandingbankListsr as V;

class TrandingbankListsr
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if (!$validate->scene('add')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        try {
            M::create($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 编辑
    public static function goEdit($data, $id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if (!$validate->scene('edit')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        try {
            M::update($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 状态
    public static function goStatus($data, $id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data,
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg' => '数据不存在', 'code' => 201];
        try {
            M::destroy($ids);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 批量修改状态
    public static function goBatchStatus($ids)
    {
        if (!is_array($ids) || empty($ids)) return ['msg' => '数据不存在', 'code' => 201];

        try {
            foreach ($ids as $id) {
                $model = M::find($id);
                if ($model && !$model->isEmpty()) {
                    if ($model["goodstatus"] == 1) { //已售状态不修改 已下架状态不修改
                        $model->save([
                            'goodstatus' => 3,
                        ]);
                        $goodstr = '1,' . $model["shellgoodsid"] . ',' . $model["shellgoodsnum"];
                        M::sendMail($model["shellplayserverid"], $model["shellplayactorid"], $model["shellgoodsname"], $goodstr);
                    }
                } else {
                    return ['msg' => "ID为 $id 的数据不存在", 'code' => 201];
                }
            }
        } catch (\Exception $e) {
            return ['msg' => '操作失败 ' . $e->getMessage(), 'code' => 201];
        }

        return ['msg' => '批量修改状态成功', 'code' => 200];
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()) {
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg' => '参数错误', 'code' => '201'];
            try {
                if (Request::param('type')) {
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach ($data as $k) {
                        $k->restore();
                    }
                } else {
                    M::destroy($ids, true);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
            return ['msg' => '操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');

        //按出售物品玩家账号查找
        if ($sellplayaccount = input("sellplayaccount")) {
            $where[] = ["sellplayaccount", "like", "%" . $sellplayaccount . "%"];
        }
        //按出售物品玩家actorid查找
        if ($shellplayactorid = input("shellplayactorid")) {
            $where[] = ["shellplayactorid", "like", "%" . $shellplayactorid . "%"];
        }
        //按出售物品玩家昵称查找
        if ($shellplaynikename = input("shellplaynikename")) {
            $where[] = ["shellplaynikename", "like", "%" . $shellplaynikename . "%"];
        }
        //按出售物品玩家区服查找
        if ($shellplayserverid = input("shellplayserverid")) {
            $where[] = ["shellplayserverid", "like", "%" . $shellplayserverid . "%"];
        }
        //按出售物品玩家上架时间查找
        $start = input("get.shellplaygrotime-start");
        $end = input("get.shellplaygrotime-end");
        if ($start && $end) {
            $where[] = ["shellplaygrotime", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        //按出售物品id查找
        if ($shellgoodsid = input("shellgoodsid")) {
            $where[] = ["shellgoodsid", "like", "%" . $shellgoodsid . "%"];
        }
        //按出售物品名称查找
        if ($shellgoodsname = input("shellgoodsname")) {
            $where[] = ["shellgoodsname", "like", "%" . $shellgoodsname . "%"];
        }
        //按出售物品数量查找
        if ($shellgoodsnum = input("shellgoodsnum")) {
            $where[] = ["shellgoodsnum", "like", "%" . $shellgoodsnum . "%"];
        }
        //按出售物品价格查找
        if ($shellgoodsprice = input("shellgoodsprice")) {
            $where[] = ["shellgoodsprice", "like", "%" . $shellgoodsprice . "%"];
        }
        //按出售物品手续费查找
        if ($goodshandingfees = input("goodshandingfees")) {
            $where[] = ["goodshandingfees", "like", "%" . $goodshandingfees . "%"];
        }
        //按出售物品状态 1.在售 2.已售查找
        if ($goodstatus = input("goodstatus")) {
            $where[] = ["goodstatus", "like", "%" . $goodstatus . "%"];
        }
        //按购买玩家账号查找
        if ($buyplayaccount = input("buyplayaccount")) {
            $where[] = ["buyplayaccount", "like", "%" . $buyplayaccount . "%"];
        }
        //按购买玩家actorid查找
        if ($buyplayactorid = input("buyplayactorid")) {
            $where[] = ["buyplayactorid", "like", "%" . $buyplayactorid . "%"];
        }
        //按购买玩家昵称查找
        if ($buyplaynikename = input("buyplaynikename")) {
            $where[] = ["buyplaynikename", "like", "%" . $buyplaynikename . "%"];
        }
        //按购买玩家区服查找
        if ($buyplayserverid = input("buyplayserverid")) {
            $where[] = ["buyplayserverid", "like", "%" . $buyplayserverid . "%"];
        }
        //按购买玩家的购买价格查找
        if ($buyplaygoodsprice = input("buyplaygoodsprice")) {
            $where[] = ["buyplaygoodsprice", "like", "%" . $buyplaygoodsprice . "%"];
        }
        //按玩家购买时间查找
        $start = input("get.buyplaygortime-start");
        $end = input("get.buyplaygortime-end");
        if ($start && $end) {
            $where[] = ["buyplaygortime", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}
